﻿using System.Windows;
using NavigateExample.Services;
using NavigateExample.Services.Navigation;
using NavigateExample.Services.Dialog;
using NavigateExample.ViewModels;
using NavigateExample.Views;

namespace NavigateExample
{
    public partial class App : Application
    {
        public IServiceProvider Services { get; }
        public static new App Current => (App)Application.Current;

        public App()
        {
            var container = new ServiceCollection();
            // 注册主窗口和ViewModel
            container.AddSingleton<MainView>();
            container.AddSingleton<MainViewModel>();
            // 注册导航服务
            container.AddSingleton<INavigationService, NavigationService>();
            // 注册对话框服务
            container.AddSingleton<IDialogService, DialogService>();
            // 注册Views和ViewModels
            container.AddTransient<LoginView>();
            container.AddTransient<LoginViewModel>();
            container.AddTransient<HomeView>();
            container.AddTransient<HomeViewModel>();
            // 注册对话框View和ViewModel
            container.AddTransient<AboutDialogView>();
            container.AddTransient<AboutDialogViewModel>();
            
            Services = container.BuildServiceProvider();
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            var mainView = Services.GetRequiredService<MainView>();
            MainWindow = mainView;
            
            // 在主窗口加载完成后导航到初始页面
            mainView.Loaded += (sender, args) =>
            {
                var navigationService = Services.GetRequiredService<INavigationService>();
                navigationService.Navigate<LoginView>();
            };
            MainWindow.Show();
        }
    }
}
